home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Tele / M / ModemInit V1.1.cpt / Modem Initializer 1.1 ƒ / Modem Init.pas < prev    next >
Pascal/Delphi Source File  |  1991-04-18  |  9KB  |  181 lines

  1. unit ModemInit;            {This is the actual init source code}
  2.  
  3. interface
  4.     uses
  5.         Serial;
  6.     const
  7.  
  8.         BAUD12 = 1;                {Value SERC resource holds for 1200 baud rate after decoding}
  9.         BAUD24 = 2;                {Value SERC resource holds for 2400 baud rate after decoding}
  10.         BAUD96 = 4;                {Value SERC resource holds for 9600 baud rate after decoding}
  11.         PARNONE = 32;            {Value SERC resource holds for no parity after decoding}
  12.         PAREVEN = 64;            {Value SERC resource holds for even parity after decoding}
  13.         PARODD = 128;            {Value SERC resource holds for odd parity after decoding}
  14.         STOP1 = 4;                {Value SERC resource holds for 1 stop bit after decoding}
  15.         STOP12 = 8;                {Value SERC resource holds for 1.5 stop bits after decoding}
  16.         STOP2 = 16;                {Value SERC resource holds for 2 stop bits after decoding}
  17.         DAT7 = 1;                {Value SERC resource holds for 7 data bits after decoding}
  18.         DAT8 = 2;                {Value SERC resource holds for 8 data bits after decoding}
  19.  
  20.     type
  21.         SerConRec = record
  22.                 baudRateNPort: Signedbyte;    {This will hold the byte containing the baud rate & Port setting when we read it from the resource}
  23.                 rest: Signedbyte;            {This will hold the encoded byte for parity, stop bits and data bits after reading the resource}
  24.             end;
  25.         SerConPtr = ^SerConRec;
  26.         SerConHnd = ^SerConPtr;    {This will be a handle to the serial configuration record}
  27.  
  28.     procedure main;                {This is the entry point into the INIT}
  29.  
  30. implementation
  31. {These forward declarations are neccesary in order to keep 'Main' as the first code in the code resource}
  32.  
  33. {==================================================================================}
  34.     procedure ShowINIT (iconID: Integer; moveX: Integer);        {This is the code to show an icon at startup}
  35.     external;        {It is in the library ShowInitIcon.lib and is the compiled version of CShowInit by Paul Mercer}
  36.                      {Darin Adler, and Paul Snively from an idea by Steve Capps.  Translated by Ken McLeod}
  37.  
  38.     procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
  39.     forward;
  40.  
  41.     procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
  42.     forward;
  43.  
  44.     function SendCommand (Baud, Par, StopB, DataB, Port: integer; CommandLine: StringHandle): Boolean;
  45.     forward;
  46. {==================================================================================}
  47.  
  48. {**** As in the cdev, Main MUST be the first code in the code resource ****}
  49.     procedure main;
  50.         var
  51.             CommandLine: StringHandle;            {The commandLine}
  52.             ConfigHnd: SerConHnd;                    {A handle to the serial configuration resource}
  53.             Baud, Par, StopB, DataB, Port: integer;        {These are the values read from the SERC resource}
  54.  
  55.     begin
  56.         ReadResources(CommandLine, ConfigHnd);                                        {Read the resources into memory}
  57.         HLock(Handle(ConfigHnd));                                                        {Lock down the handles so they don't get trashed}
  58.         HLock(Handle(CommandLine));
  59.         DecodeConfigResource(ConfigHnd, Baud, Par, StopB, DataB, Port);            {Decode the resources so we can use them}
  60.         if not SendCommand(Baud, Par, StopB, DataB, Port, CommandLine) then        {Send the command}
  61.             ShowINIT(-4033, -1)                {If we had an error on sending the command,  show the icon with an x through it}
  62.         else
  63.             ShowINIT(-4064, -1);                {If we had no error on sending the command,  show the icon without an x through it}
  64.         HUnLock(Handle(ConfigHnd));                                                    {We are done with the handles so let them go}
  65.         HUnLock(Handle(CommandLine));
  66.     end;
  67. {------------------------------------------------------------------------}
  68.  
  69.     procedure ReadResources (var CommandLine: StringHandle; var ConfigHnd: SerConHnd);
  70.  
  71.         const
  72.             CONFIGID = -4033;    {These are the same as the cdev resources}
  73.             COMMID = -4033;
  74.         var
  75.             tempH: Handle;        {Temporary storage for reading the SERC Resource}
  76.  
  77.     begin
  78.         CommandLine := GetString(COMMID);                        {Get the command line resource}
  79.         tempH := GetResource('SERC', CONFIGID);                    {Get the serial configuration resource}
  80.         ConfigHnd := SerConHnd(TempH);                            {Typecast generic handle to one the cdev will understand}
  81.     end;
  82. {------------------------------------------------------------------------}
  83.  
  84.     procedure DecodeConfigResource (ConfigHnd: SerConHnd; var Baud, Par, StopB, DataB, Port: integer);
  85.  
  86.     begin
  87.         with ConfigHnd^^ do
  88.             begin
  89.                 Baud := integer(BAND(baudRateNPort, $0F));    {Extract the baud rate as a value the rest of the init will recognize}
  90.                 Port := integer(BAND(baudRateNPort, $F0));    {Extract the port to be used as a value the rest of the init will recognize}
  91.                 Par := integer(BAND(rest, $E0));                {Extract the parity value as a value the rest of the init will recognize}
  92.                 StopB := integer(BAND(rest, $1C));            {Extract the stop bits value as a value the rest of the init will recognize}
  93.                 DataB := integer(BAND(rest, $03));            {Extract the data bits value as a value the rest of the init will recognize}
  94.             end;
  95.     end;
  96. {------------------------------------------------------------------------}
  97.  
  98.     function SetSerialConfig (Baud, Par, StopB, DataB: integer): integer;
  99.         var
  100.             B, P, S, D: integer;
  101.             {Temporary storage to hold the toolbox values of baud rate, parity, stop bits and data bits for SerReset}
  102.  
  103.     begin
  104.         case Baud of
  105.             BAUD12: 
  106.                 B := Baud1200;        {If the baud rate is 1200, set the baud rate value for serial configuration}
  107.             BAUD24: 
  108.                 B := Baud2400;        {If the baud rate is 2400, set the baud rate value for serial configuration}
  109.             BAUD96: 
  110.                 B := Baud9600;        {If the baud rate is 9600, set the baud rate value for serial configuration}
  111.         end;
  112.  
  113.         case Par of
  114.             PARNONE: 
  115.                 P := NOParity;            {If there is no parity, set the parity value for serial configuration}
  116.             PAREVEN: 
  117.                 P := EVENParity;            {If there is even parity, set the parity value for serial configuration}
  118.             PARODD: 
  119.                 P := ODDParity;            {If there is odd parity, set the parity value for serial configuration}
  120.         end;
  121.  
  122.         case StopB of
  123.             STOP1: 
  124.                 S := Stop10;                {If there is 1 stop bit, set the Stop Bits value for serial configuration}
  125.             STOP12: 
  126.                 S := Stop15;                {If there are 1.5 stop bits, set the Stop Bits value for serial configuration}
  127.             STOP2: 
  128.                 S := stop20;                {If there are 2 stop bits, set the Stop Bits value for serial configuration}
  129.         end;
  130.  
  131.         case DataB of
  132.             DAT7: 
  133.                 D := Data7;                {If there are 7 data bits, set the Data Bits value for serial configuration}
  134.             DAT8: 
  135.                 D := Data8;                {If there are 8 data bits, set the Data Bits value for serial configuration}
  136.         end;
  137.  
  138.         SetSerialConfig := B + P + S + D;        {This is the actual serial configuration value sent to the modem}
  139.     end;
  140. {------------------------------------------------------------------------}
  141.  
  142.     function SendCommand (Baud, Par, StopB, DataB, Port: integer; CommandLine: StringHandle): Boolean;
  143.  
  144.         var
  145.             serConfig: integer;                {This is the serial port configuration value}
  146.             refnum: integer;                {This is the reference number for the modem serial out port}
  147.             Len: Longint;                    {The Length of the string}
  148.             Error: OSErr;                    {Will hold the error code if we have an error}
  149.             tempstr: str255;                {Will hold the command line}
  150.             TempLong: Longint;                {A temporary long integer used to hold the value of cdevStorage}
  151.             tempStr2: str255;
  152.     begin
  153.         SendCommand := False;                            {Set initial value to false to signify non-completion}
  154.         if (Port = 16) then
  155.             Error := OpenDriver('.AOut', refnum)                                {Open the modem out port}
  156.         else
  157.             Error := OpenDriver('.BOut', refnum);                                {Open the printer out port}
  158.         if Error <> noErr then            {Check for an error}
  159.             exit(SendCommand);                            {We had an error so leave the routine}
  160.         serConfig := SetSerialConfig(Baud, Par, StopB, DataB);            {Set up configuration}
  161.         Error := SerReset(refnum, serConfig);                            {Set the configuration}
  162.         if Error <> noErr then            {Check for an error}
  163.             exit(SendCommand);                            {We had an error so leave the routine}
  164.         tempStr := CommandLine^^;                            {Get the command line}
  165.         Len := length(tempStr);                                                {Get the length of the command}
  166.         if ord(tempStr[Len]) <> 13 then
  167.             begin
  168.                 tempStr := concat(tempStr, Chr(13));                    {If there isn't a <CR> at the end of the command line, put one on}
  169.                 Len := Len + 1;                                            {Add one to the length of the string since we added a <CR>}
  170.             end;
  171.  
  172.         Error := FSWrite(refNum, Len, @tempStr[1]);                    {Write the command line to the modem port}
  173.         if Error <> noErr then            {Check for an error}
  174.             exit(SendCommand);                            {We had an error so leave the routine}
  175.         Error := CloseDriver(refNum);                                      {Close the port}
  176.         if Error <> noErr then            {Check for an error}
  177.             exit(SendCommand);                            {We had an error so leave the routine}
  178.         SendCommand := True;                    {We made it through without an error -- Let the calling routine know it}
  179.     end;
  180. {------------------------------------------------------------------------}
  181. end.            {Unit Modem Init}